package com.fzk.blog.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fzk.blog.bean.Type;
import com.fzk.blog.bean.vo.TypeInfo;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author fzk
 * @date 2021-08-15 16:05
 */
public interface TypeMapper extends BaseMapper<Type> {
    @Select("Select * from tbl_type " +
            " where name=#{name} and flag=0")
    List<Type> getTypeByName(String name);

    @Select("SELECT t.id,t.name,t.description,COUNT(b.id) as blogNumber " +
            "   FROM tbl_type t LEFT JOIN tbl_blog b ON (t.id=b.type_id AND b.flag=0 AND b.published=TRUE) " +
            "   WHERE t.flag=0 " +
            "   GROUP BY t.id " +
            "   ORDER BY blogNumber DESC")
    List<TypeInfo> getTypeInfoList();

    @Select("SELECT t.id,t.name,t.description,COUNT(b.id) as blogNumber " +
            "   FROM tbl_type t LEFT JOIN tbl_blog b ON (t.id=b.type_id AND b.flag=0 AND b.published=TRUE) " +
            "   WHERE t.flag=0 AND t.id=#{id} " +
            "   GROUP BY t.id " +
            "   ORDER BY blogNumber DESC")
    TypeInfo getTypeInfoById(int id);

    @Update("UPDATE tbl_type SET edit_by=#{loginId},flag=1 WHERE id=#{typeId} AND flag=0 ")
    int logicalDeleteById(int typeId,int loginId);
}
